package com.lidy.tankbattle.tankgame1;

import com.lidy.tankbattle.tankgame1.constant.BulletPriorities;
import com.lidy.tankbattle.tankgame1.constant.TankPriorities;
import lombok.Data;

/**
 * @author lidy
 * @version V1.0
 * @classname Bullet
 * @description 坦克子弹类
 * @date 2022/5/31 18:56
 */
@Data
public class Bullet implements Runnable {

    /**
     * 子弹的X坐标
     **/
    private int x;

    /**
     * 子弹的Y坐标
     **/
    private int y;

    /**
     * 子弹方向
     **/
    private int direction;

    /**
     * 子弹的速度
     **/
    private int speed = BulletPriorities.BULLET_INIT_SPEED;

    /**
     * 子弹是否存活
     **/
    private Boolean live = true;

    /**
     * 子弹所属的坦克编号
    **/
    private int fromTankNumber;

    /**
     * 子弹所属的坦克类型
     **/
    private int fromTankType;

    /**
     * 子弹的威力
    **/
    private int power = BulletPriorities.BULLET_POWER;

    /**
     * 子弹图片地址
    **/
    private String imageAddress;

    public Bullet(int x, int y, int direction, int tankType, int tankNumber) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.fromTankNumber = tankNumber;
        if (tankType == TankPriorities.MY_TANK_TYPE) {
            imageAddress = "/images/bullet/bulletYellow.gif";
        } else {
            imageAddress = "/images/bullet/bulletGreen.gif";
        }
    }

    @Override
    public void run() {
        while (live) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            switch (direction) {
                // 向上
                case TankPriorities.DIRECTION_UP -> y -= speed;

                // 向下
                case TankPriorities.DIRECTION_DOWN -> y += speed;

                // 向左
                case TankPriorities.DIRECTION_LEFT -> x -= speed;

                // 向右
                case TankPriorities.DIRECTION_RIGHT -> x += speed;
                default -> {
                }
            }
            if (x <= 0 || x >= TankPriorities.PANEL_WIDTH || y <= 0 || y >= TankPriorities.PANEL_HEIGHT) {
                live = false;
                break;
            }
        }
    }
}
